package com.sofwin.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.pojo.SysDept;
import com.sofwin.pojo.SysUser;
import com.sofwin.service.DeptService;
import com.sofwin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * ClassName: UserController
 * Description: TODO
 * date: 2022/4/16 2:39 下午
 *
 * @author andyliu
 * @qq 115176513
 * @since JDK 1.8
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;

    /**
     * 跳转到列表页
     * @return
     */
    @GetMapping
    public String data(SysUser user, @RequestParam(required = true,defaultValue = "1")
                       Integer pageNumber, @RequestParam(required = true,defaultValue = "5")
                       Integer pageSize, ModelMap map){
        PageInfo<SysUser> pageInfo = userService.queryUsers(user, pageNumber, pageSize);
        map.addAttribute("page",pageInfo);
        return "user/data";
    }
    @GetMapping("toEdit/{id}")
    public String toEdit(@PathVariable Integer id, Model model){
        SysUser user = userService.queryById(id);
        model.addAttribute("user",user);
        List<SysDept> depts = deptService.queryDetps(null);
        model.addAttribute("depts",depts);
        return "user/edit";
    }
}
